/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservices;

/**
 *
 * @author BENAMMAR Riyadh
 */
public class Distance {
    Ville ville1;
    Ville ville2;
    
    public Distance(Ville v1,Ville v2){
        ville1=v1;
        ville2=v2;
    }
    
    public double calculDistance(){
        return distFrom(ville1.getLatitude(), ville1.getLongetude(), ville2.getLatitude(), ville2.getLongetude());
    }
    
    //méthode importée du forum StackOverFlow de l'auteur "Espen Herseth Halvorsen"
    public double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
               Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    double kmMeterConversion = 1.609;
    return (dist * kmMeterConversion);
    }
    
    public static void main(String[] args) {
        Ville c1=new Ville("Montreal");
        Ville c2=new Ville("Paris");
        Distance d=new Distance(c1, c2);
        double dist=d.calculDistance();
        System.out.println("la distance entre mont et paris est: "+dist);
    }
}
